//
//  VCOfDiscover.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/27.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfDiscover.h"
#import "XYMacro.h"
#import "DiscoverModelFirst.h"

@interface VCOfDiscover () <TopPageBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) DiscoverTopPageBar *topBar;
@property (nonatomic, strong) UICollectionView *collectionViewOfContent;
@property (nonatomic, strong) DiscoverModelFirst *firstModel;
@property (nonatomic, strong) DiscoverModelSecond *secondModel;

@end

@implementation VCOfDiscover

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self configNavigationBar];
    [self configViewController];
    [self createKVO];

}

- (void)handleData {
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/discovery.jhtml" andBody:@"json=%7B%7D" andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _firstModel = [DiscoverModelFirst modelWithDictionary:result];
        [_collectionViewOfContent reloadData];
    } andFailureBlock:^(NSError *error) {}];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channellist.jhtml" andBody:@"json=%7B%7D"andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) { 
        _secondModel = [DiscoverModelSecond modelWithDictionary:result];
        [_collectionViewOfContent reloadData];
    } andFailureBlock:^(NSError *error) {}];
    
}


- (void)configNavigationBar {
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    titleLabel.text = @"发现";
    titleLabel.textColor = [UIColor whiteColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *seacherButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [seacherButton setImage:[UIImage imageNamed:@"menu_icon_4_1"] forState:UIControlStateNormal];
    seacherButton.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:seacherButton];
    [seacherButton addTarget:self action:@selector(seacherPage:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
}

- (void)configViewController {
    self.view.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
    [self createTopPageBar];
    [self createContentView];
}

- (void)createTopPageBar {
    _topBar = [[DiscoverTopPageBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) withTitleArr:@[@"精选集", @"分类"]];
    _topBar.delegate = self;
    [self.view addSubview:_topBar];
}

- (void)createContentView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64 - 49);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionViewOfContent = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64 - 49) collectionViewLayout:layout];
    _collectionViewOfContent.dataSource = self;
    _collectionViewOfContent.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
    _collectionViewOfContent.delegate = self;
    _collectionViewOfContent.pagingEnabled = YES;
    [self.view addSubview:_collectionViewOfContent];
    [_collectionViewOfContent registerClass:[DiscoverCollectionViewCellOfContentView class] forCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentView"];
    [_collectionViewOfContent registerClass:[DiscoverCollectionViewCellOfContentViewSecond class] forCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewSecond"];
}





- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (_firstModel.data.special_list.count != 0) {
        return 2;
    }else {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DiscoverCollectionViewCellOfContentView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentView" forIndexPath:indexPath];
        cell.model = _firstModel;
        return cell;
    } else {
        DiscoverCollectionViewCellOfContentViewSecond *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewSecond" forIndexPath:indexPath];
        cell.model = _secondModel;
        return cell;
    }
    
}


- (void)seacherPage:(UIButton *)button {
    VCOfSeacher *nextSearchPage = [[VCOfSeacher alloc] init];
    [self.navigationController pushViewController:nextSearchPage animated:YES];
}

#pragma mark ----------- KVO 键值观察 -----------------
- (void)createKVO {
    //KVO
    [_collectionViewOfContent addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat oldX = [[change objectForKey:@"old"] CGPointValue].x;
    
    CGFloat redX = (newX / SCREEN_WIDTH) * (SCREEN_WIDTH / 2);
    // 修改Redline位置.
    self.topBar.redLineView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
     DiscoverCollectionViewCellForTopPageBar *cell1 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:newX / SCREEN_WIDTH inSection:0]];
    DiscoverCollectionViewCellForTopPageBar *cell2 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:oldX / SCREEN_WIDTH inSection:0]];
    cell2.titleLabel.textColor = [UIColor grayColor];
    cell1.titleLabel.textColor = [UIColor whiteColor];
}


#pragma mark --------------- TopBarDelegate---------------
- (void)didClickItemindexPath:(NSIndexPath *)indexPath PageBar:(DiscoverTopPageBar *)pageBar {
    [UIView animateWithDuration:0.3 animations:^{
        _collectionViewOfContent.contentOffset = CGPointMake(indexPath.row * SCREEN_WIDTH, 0);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
